<HTML>
<HEAD>
<SCRIPT src="../pidcrypt.js"></SCRIPT>
<SCRIPT src="../pidcrypt_util.js"></SCRIPT>
<SCRIPT src="../md5.js"></SCRIPT><!--needed for key and iv generation-->
<SCRIPT src="../aes_core.js"></SCRIPT><!--needed block en-/decryption-->
<SCRIPT src="../aes_cbc.js"></SCRIPT><!--needed for cbc mode-->
<SCRIPT>

function passed(testFunction){
  var span = document.getElementById(testFunction);
  span.innerHTML = 'passed'
  span.style.color = '#00C000';
}


function compute(testFunction){
  var password = 'password';
  var plain = 'Text to encrypt ...';
  var bits = 256;
  var aes = new pidCrypt.AES.CBC();
  var crypted ='U2FsdGVkX184Cfnm6ylziwf1VJ2Pz0AM4bjRKRvQw+/Zt6HVCYboI/Q3arfKymzn';
  var cryptedRaw = 'e+yw1TEVp/m+w2yUg20Thy7N9T2n2lvV/K0tAV+UQDA=';
  var key = '8f66bc54ec5f9988f662cb75284eaa4e64879467ca852b94376a9ab54eae85ba';
  var iv = 'eb5eb5ac35caf38dfc0ffcf50c1fb61e';
  var result ='';
  var options = {nBits:bits};
  var ok = false;
  switch(testFunction) {
    case 'init':
        aes.init(password, options);
        crypted = aes.encrypt(plain);
        aes.initDecrypt(crypted, password, options);
        result = aes.decrypt();
        ok = (plain == result);
      break;
    case 'initDecrypt':
        aes.initDecrypt(crypted, password, options);
        result = aes.decrypt();
        ok = (plain == result);
      break;
    case 'initEncrypt':
        aes.initEncrypt(plain, password, options);
        crypted = aes.encrypt();
        aes.initDecrypt(crypted, password, options);
        result = aes.decrypt();
        ok = (plain == result);
      break;
    case 'initByValues':
        aes.initByValues(plain, key, iv);
        crypted = aes.encrypt();
        aes.initByValues(crypted, key, iv);
        result = aes.decrypt();
        ok = (plain == result);
      break;
    case 'createKeyAndIv':
        result = aes.createKeyAndIv({password:password, salt: '12345678', bits: bits});
        ok = (result.key == key && iv == result.iv);
      break;
    case 'encryptRaw':
        var plainRaw = pidCryptUtil.toByteArray(plain);
        aes.initByValues('', key, iv);
        crypted = aes.encryptRaw(plainRaw);
        ok = (crypted ==  pidCryptUtil.decodeBase64(cryptedRaw));
      break;
    case 'encrypt':
    case 'decrypt':
        aes.initByValues(plain, key, iv);
        crypted = aes.encrypt();
        aes.initByValues(crypted, key, iv);
        result = aes.decrypt();
        ok = (plain == result);
      break;
    case 'encryptText':
        crypted = aes.encryptText(plain, password, options);
        result = aes.decryptText(crypted, password, options);
        ok = (plain == result);
      break;
    case 'decryptRaw':
        aes.initByValues('', key, iv);
        result = aes.decryptRaw(pidCryptUtil.toByteArray(pidCryptUtil.decodeBase64(cryptedRaw)));
        ok = (plain == result);
      break;
    case 'decryptText':
        result = aes.decryptText(crypted, password, options);
        ok = (plain == result);
      break;
  }
  if(ok)
    passed(testFunction);
  else
    document.getElementById('report').innerHTML += '<P><h4>' + testFunction + '</H4>' + aes.pidcrypt.getAllMessages({lf:'<BR>'}) + '</P>';
}

function testAll(){
  compute('init');
  compute('initEncrypt');
  compute('initDecrypt');
  compute('initByValues');
  compute('createKeyAndIv');
  compute('encryptRaw');
  compute('encrypt');
  compute('encryptText');
  compute('decryptRaw');
  compute('decrypt');
  compute('decryptText');

}
</SCRIPT>
</HEAD>
<BODY ONLOAD="testAll();">

<H2><A HREF="?page=aes-cbc">AES-CBC</A> En-/Decryption test</H2>
Please visit  <a href="http://www.pidder.com/pidcrypt">pidCrypt Home</a> for actual online demo and latest version!
<UL>
  <LI>init: <span ID="init" style="color:#FF0000">failed</span></LI>
  <LI>initEncrypt: <span ID="initEncrypt" style="color:#FF0000">failed</span></LI>
  <LI>initDecrypt: <span ID="initDecrypt" style="color:#FF0000">failed</span></LI>
  <LI>initByValues: <span ID="initByValues" style="color:#FF0000">failed</span></LI>
  <LI>createKeyAndIv: <span ID="createKeyAndIv" style="color:#FF0000">failed</span></LI>
  <LI>encryptRaw: <span ID="encryptRaw" style="color:#FF0000">failed</span></LI>
  <LI>encrypt: <span ID="encrypt" style="color:#FF0000">failed</span></LI>
  <LI>encryptText: <span ID="encryptText" style="color:#FF0000">failed</span></LI>
  <LI>decryptRaw: <span ID="decryptRaw" style="color:#FF0000">failed</span></LI>
  <LI>decrypt: <span ID="decrypt" style="color:#FF0000">failed</span></LI>
  <LI>decryptText: <span ID="decryptText" style="color:#FF0000">failed</span></LI>
</UL>
<H3>Report</H3>
<DIV id="report"></DIV>
</BODY>
</HTML>